package com.lipi.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

//校验器，

/**
 * ListValue:校验注解
 * Integer：值类型
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    //存储客户端的注解值
    private Set<Integer> set = new HashSet<>();

    //初始化方法,会读取使用该注解的vals属性值
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (Integer i : vals
        ) {
            set.add(i);
        }
    }

    //判断是否校验成功，布尔类型

    /**
     * @param value                      前端传入的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(value);
    }


}
